<?php
/**	
*	FILENAME: 		/server_app/person.php
*	DESCRIPTION:	This backend script produces a JSON response representing the LDAP directory information for a single person.
*	AUTHOR:			Casey Burkhardt
*	VERSION:		1.0.0
*	LAST MODIFIED:	12/30/2009
**/

require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/ldap.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");

enforceSSL();

$userName = $_POST["username"];

if (strlen($userName) == 0) {
	$response["response_type"] = "error";
	$response["error_desc"] = "No Username Provided.";
	echo(json_encode($response));
	die();
} else {
	$account_type = ldap_record_type($userName);
	if ($account_type == false)	{
		$response["response_type"] = "error";
		$response["error_desc"] = "Could not determine account type for username " . $userName . ".";
		echo(json_encode($response));
		die();
	} elseif ($account_type == "Student") {
		// Students created before 2008 (Test case: $userName = cburkh04)
		$lead_nodes1 = "ou=" . strtoupper(substr($userName, 0,1)) . ", ou=Students, o=villanova.edu";
		$result1 = ldap_record($lead_nodes1, $userName);

		// Students created after 2008 (Test case: $userName = tclift01)
		$lead_nodes2 = "ou=People, o=villanova.edu";
		$result2 = ldap_record($lead_nodes2, $userName);

		// One of those queries failed, remove the failing result
		if ($result1 == false) {
			$result = $result2;
		} else {
			$result = $result1;
		}
		
		// Check for the invalid result error condition
		if($result == false) {
			$response["response_type"] = "error";
			$response["error_desc"] = "Could not retrieve LDAP record for username " . $userName . ".";
			echo(json_encode($response));
			die();
		}

		// Parse out the fields required for Students and create the JSON result.
		$response = array(
			"response_type"       => "success",
			"classification"      => "student",
			"name"                => $result[0]["cn"][0],
			"student_class"       => $result[0]["studentclass"][0],
			"student_college"     => $result[0]["studentcollege"][0],
			"department"          => $result[0]["studentmajor"][0],
			"position"            => NULL,
			"building"            => $result[0]["building"][0],
			"room_number"         => $result[0]["roomnumber"][0],
			"phone_number"        => NULL,
			"email"               => $result[0]["mail"][0],
			"homepage"            => $result[0]["labeleduri"][0]
		);
		if ($response["building"] == "XXX")	{
			$response["building"] = NULL;
		}
		if ($response["room_number"] == "XXX") {
			$response["room_number"] = NULL;
		} 
		echo(json_encode($response));
		die();
	} elseif ($account_type == "Faculty") {
		$lead_nodes = "ou=Faculty, ou=Employees, o=villanova.edu";
		$result = ldap_record($lead_nodes, $userName);

		// Check for the invalid result error condition
		if ($result == false) {
			$response["response_type"] = "error";
			$response["error_desc"] = "Could not retrieve LDAP record for user ID " . $userName . ".";
			echo(json_encode($response));
			die();
		}

		// Parse out the fields required for Faculty and create the JSON result.
		$response = array(
			"response_type"       => "success",
			"classification"      => "faculty",
			"name"                => $result[0]["cn"][0],
			"student_class"       => NULL,
			"student_college"     => NULL,
			"department"          => $result[0]["departmentname"][0],
			"position"            => $result[0]["title"][0],
			"building"            => $result[0]["building"][0],
			"room_number"         => $result[0]["roomnumber"][0],
			"phone_number"        => $result[0]["telephonenumber"][0],
			"email"               => $result[0]["mail"][0],
			"homepage"            => $result[0]["labeleduri"][0]
		);
		echo(json_encode($response));
		die();
	} elseif ($account_type == "Staff") {
		$lead_nodes = "ou=Staff, ou=Employees, o=villanova.edu";
		$result = ldap_record($lead_nodes, $userName);

		// Check for the invalid result error condition
		if ($result == false) {
			$response["response_type"] = "error";
			$response["error_desc"] = "Could not retrieve LDAP record for user ID " . $userName . ".";
			echo(json_encode($response));
			die();
		}

		// Parse out the fields required for Staff and create the JSON result.
		$response = array(
			"response_type"       => "success",
			"classification"      => "staff",
			"name"                => $result[0]["cn"][0],
			"student_class"       => NULL,
			"student_college"     => NULL,
			"department"          => $result[0]["departmentname"][0],
			"position"            => $result[0]["title"][0],
			"building"            => $result[0]["building"][0],
			"room_number"         => $result[0]["roomnumber"][0],
			"phone_number"        => $result[0]["telephonenumber"][0],
			"email"               => $result[0]["mail"][0],
			"homepage"            => $result[0]["labeleduri"][0]
		);
		echo(json_encode($response));
		die();
	} else {
		// Unknown LDAP Person Type
		$response["response_type"] = "error";
		$response["error_desc"] = "Nondeterministic error parsing LDAP record for username " . $userName . ".";
		echo(json_encode($response));
		die();
	}
}
?>